<?php
/*
Author: Martin Wiso
Author URI: http://www.wiso.cz/
Description: Administrative options for Custom Logo
*/

// load UI helper
require_once('wpui.class.php');

$path = 'wp-content/plugins/custom-logo';
$location = get_option('siteurl').'/wp-admin/admin.php?page=custom-logo/custom-logo-options.php';
$imagesDir = get_option('siteurl').'/'.$path.'/images/';

// create HTML for with logos based on WP version
$wp_version = get_bloginfo('version');
$logos = array();
if ($wp_version >= 2.5) {
	$logos = array(	'wp-2.0-button-trans.gif', 
					'wp-2.0-button-small-trans.gif', 
					'dandy-logo.png'
				  );
} else if ($wp_version >= 2.1) {
	$logos = array(	'wp-2.0-button-trans.gif', 
					'wp-2.0-button-small-trans.gif', 
					'dandy-logo.png', 
					'wp-2.0-square-button-trans.gif'
				  );
} else {
	$logos = array(	'wp-2.0-button2.gif', 
					'wp-2.0-square-button-trans.gif', 
					'wp-2.0-button.gif'
				  );
}
$buttonsArea = '<table width="100%" cellpadding="5" class="form-table" border="0">
		       <tr valign="middle">';
foreach ($logos as $logo) {
	$buttonsArea .= sprintf('<td align="center">
		<a href="javascript:void(0);" onclick="return WPCustomLogo.setSample(\'%s\');" title="Select by clicking here"><img src="%s/%s" border="0" /></a>
	</td>%s', $logo, $imagesDir, $logo, "\n");
}
$buttonsArea .= '</table>
<p>You can choose between these sample logo for main logo. Just click on logo you like and update options.</p>';

// plugin description based on WP version
$description = '';
if ($wp_version >= 2.5) {	     
	$description = 'Original WordPress logo size is width 290px and height 66px. Because you are using WordPress '.$wp_version.', be aware of different height (change from 2.5)!';
} else if ($wp_version >= 2.1) {	     
	$description = 'Original WordPress logo size is width 250px and height 68px. Because you are using WordPress '.$wp_version.', your new logo image must have transparent background 
		     	 (changed login dialog background image at 2.1)!';
} else {
	$description = 'Original WordPress logo size is width 250px and height 68px.';
}

// setup UI 
$options = array('General' => array(	array(	'name' 		=> 'wpcl_custom_logo_path',
												'label' 	=> 'Path to logo image', 
												'type'		=> 'text', 
												'value' 	=> get_option('wpcl_custom_logo_path'),
												'tooltip' 	=> 'Leave form field empty to restore default logo.',
												'size'		=> 95),
										array (	'label'		=> 'Samples',
												'type'		=> 'html',
												'value'		=> $buttonsArea)
									));
									
$option_keys = array('wpcl_custom_logo_path');
$ui = new WPUI('Custom Logo', $description, 'wpcl', $options, $option_keys);

// check user level
if (function_exists('current_user_can') && !current_user_can('manage_options')) {
	$ui->renderError();
}
else {
	// check form submission and update options
	if ('process' == $_POST['stage']) {
		foreach ($option_keys as $option) {
			update_option($option, $_POST[$option]);
		}
	}
	
	// client code for changing value
	printf('<script type="text/javascript">
	//<![CDATA[
	var WPCustomLogo = {};
	WPCustomLogo.setSample = function (filename) {
		try {
			var obj = document.getElementById(\'wpcl_custom_logo_path\');
			if (obj) {
				obj.value = \'%s\' + filename;
			}
		}
		catch (e) { /* alert(\'Error: \' + e.description); */ }
		
		return false;
	};
	//]]>
	</script>
	', $imagesDir);
	
	// render UI
	echo $ui->render();
}

// end of script
?>